//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmLinearOpLayout.h"
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpIterator.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (Cephei::Core::IVector<UInt64>^ dim) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phFdmLinearOpLayout = NULL;
#endif
        dim->Lock();
        INativeVector<UInt64>^ _NCIdim = dim->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCdim = safe_cast<CUInt64Vector^>(_NCIdim);
        std::vector<QuantLib::Size>& _dim = static_cast<std::vector<QuantLib::Size>&> (_NCdim->GetReference ());
        _ppFdmLinearOpLayout = new boost::shared_ptr<QuantLib::FdmLinearOpLayout> (new QuantLib::FdmLinearOpLayout ( _dim ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (dim != nullptr) dim->Unlock();    //not optional
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (boost::shared_ptr<QuantLib::FdmLinearOpLayout>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpLayout = NULL;
#endif
	_ppFdmLinearOpLayout = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (QuantLib::FdmLinearOpLayout& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmLinearOpLayout = NULL;
#endif
	_ppFdmLinearOpLayout = new boost::shared_ptr<QuantLib::FdmLinearOpLayout> (&childNative);
    
    _FdmLinearOpLayoutOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (CFdmLinearOpLayout^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpLayout = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmLinearOpLayout = new boost::shared_ptr<QuantLib::FdmLinearOpLayout> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpLayout = NULL;
#endif
	if (!t->IsSubclassOf(CFdmLinearOpLayout::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (QuantLib::Handle<QuantLib::FdmLinearOpLayout>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmLinearOpLayout = &childNative;
	_ppFdmLinearOpLayout = &static_cast<boost::shared_ptr<QuantLib::FdmLinearOpLayout>>(childNative.currentLink());
    
    _FdmLinearOpLayoutOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (QuantLib::Handle<QuantLib::FdmLinearOpLayout> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmLinearOpLayout = &childNative;
	_ppFdmLinearOpLayout = &static_cast<boost::shared_ptr<QuantLib::FdmLinearOpLayout>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::CFdmLinearOpLayout (QuantLib::FdmLinearOpLayout childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmLinearOpLayout = NULL;
#endif
	_ppFdmLinearOpLayout = new boost::shared_ptr<QuantLib::FdmLinearOpLayout> (new QuantLib::FdmLinearOpLayout (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::~CFdmLinearOpLayout ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmLinearOpLayout != NULL)
    {
	    delete _ppFdmLinearOpLayout;
        _ppFdmLinearOpLayout = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::!CFdmLinearOpLayout ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmLinearOpLayout != NULL)
    {
	    delete _ppFdmLinearOpLayout;
    }
}
QuantLib::FdmLinearOpLayout& Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::GetReference ()
{
    if (_ppFdmLinearOpLayout == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmLinearOpLayout;
}
boost::shared_ptr<QuantLib::FdmLinearOpLayout>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::GetShared ()
{
    if (_ppFdmLinearOpLayout == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmLinearOpLayout;
}
QuantLib::FdmLinearOpLayout* Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::GetPointer ()
{
    if (_ppFdmLinearOpLayout == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmLinearOpLayout;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmLinearOpLayout>& Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::GetHandle ()
{
	if (_phFdmLinearOpLayout == NULL)
	{
		_phFdmLinearOpLayout = new Handle<QuantLib::FdmLinearOpLayout> (*_ppFdmLinearOpLayout);
	}
	return *_phFdmLinearOpLayout;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::HasNative () 
{
	return (_ppFdmLinearOpLayout != NULL);
}

Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Begin::get ()
{
    try
    {
    	QuantLib::FdmLinearOpIterator _rv = (QuantLib::FdmLinearOpIterator)(*_ppFdmLinearOpLayout)->begin ( );   
        Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpIterator^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpIterator (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Dim::get ()
{
    try
    {
    	std::vector<QuantLib::Size>& _rv = (std::vector<QuantLib::Size>&)(*_ppFdmLinearOpLayout)->dim ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::End::get ()
{
    try
    {
    	QuantLib::FdmLinearOpIterator _rv = (QuantLib::FdmLinearOpIterator)(*_ppFdmLinearOpLayout)->end ( );   
        Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpIterator^ _nrv = REFNEW Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpIterator (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Index (Cephei::Core::IVector<UInt64>^ coordinates)
{
    try
    {
        coordinates->Lock();
        INativeVector<UInt64>^ _NCIcoordinates = coordinates->getFeature (NativeFeature::Value);
        CUInt64Vector^ _NCcoordinates = safe_cast<CUInt64Vector^>(_NCIcoordinates);
        std::vector<QuantLib::Size>& _coordinates = static_cast<std::vector<QuantLib::Size>&> (_NCcoordinates->GetReference ());
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmLinearOpLayout)->index ( _coordinates );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (coordinates != nullptr) coordinates->Unlock(); //NO2  
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Neighbourhood (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iterator, UInt64 i1, Int32 offset1, UInt64 i2, Int32 offset2)
{
    CFdmLinearOpIterator^ _Citerator;
    try
    {
        _Citerator = safe_cast<CFdmLinearOpIterator^> (iterator);
        _Citerator->Lock();
        QuantLib::FdmLinearOpIterator& _iterator = static_cast<QuantLib::FdmLinearOpIterator&> (_Citerator->GetReference ()); 
        QuantLib::Size _i1 = (QuantLib::Size)ValueHelper::Convert (i1); //a
        QuantLib::Integer _offset1 = (QuantLib::Integer)ValueHelper::Convert (offset1); //a
        QuantLib::Size _i2 = (QuantLib::Size)ValueHelper::Convert (i2); //a
        QuantLib::Integer _offset2 = (QuantLib::Integer)ValueHelper::Convert (offset2); //a
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmLinearOpLayout)->neighbourhood ( _iterator,  _i1,  _offset1,  _i2,  _offset2 );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citerator != nullptr) _Citerator->Unlock();
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Neighbourhood (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iterator, UInt64 i, Int32 offset)
{
    CFdmLinearOpIterator^ _Citerator;
    try
    {
        _Citerator = safe_cast<CFdmLinearOpIterator^> (iterator);
        _Citerator->Lock();
        QuantLib::FdmLinearOpIterator& _iterator = static_cast<QuantLib::FdmLinearOpIterator&> (_Citerator->GetReference ()); 
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Integer _offset = (QuantLib::Integer)ValueHelper::Convert (offset); //a
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmLinearOpLayout)->neighbourhood ( _iterator,  _i,  _offset );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citerator != nullptr) _Citerator->Unlock();
    }
}
UInt64 Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFdmLinearOpLayout)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout::Spacing::get ()
{
    try
    {
    	std::vector<QuantLib::Size>& _rv = (std::vector<QuantLib::Size>&)(*_ppFdmLinearOpLayout)->spacing ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpLayout^ Cephei::QL::Methods::Finitedifferences::Operators::CFdmLinearOpLayout_Factory::Create (Cephei::Core::IVector<UInt64>^ dim)
{
    return REFNEW CFdmLinearOpLayout ( dim);
}
